package com.lv.phuotbacnam.bl.queries;

import java.util.List;

import com.lv.phuotbacnam.bl.callback.CallBackFromParse;
import com.lv.phuotbacnam.bl.data.BinhChonObject;
import com.lv.phuotbacnam.bl.log.Logger;
import com.lv.phuotbacnam.toolbox.RoundDouble;
import com.parse.FindCallback;
import com.parse.ParseException;
import com.parse.ParseObject;
import com.parse.ParseQuery;
import com.parse.ParseUser;
import com.parse.SaveCallback;

public class BinhChonQueries {

	/**
	 * Create new binh chon when just create new diadiem
	 * 
	 * @param binhChonObject
	 * @param diadiem
	 */
	public static void createNewBinhChon(BinhChonObject binhChonObject,
			ParseObject diadiem) {
		final ParseObject binhchon = new ParseObject(
				QueriesConstant.TableBinhChon.Table_Name);
		binhchon.put(QueriesConstant.TableBinhChon.COLUMN_nguoi_binh_chon,
				ParseUser.getCurrentUser());
		binhchon.put(QueriesConstant.TableBinhChon.COLUMN_dia_diem, diadiem);
		binhchon.put(QueriesConstant.TableBinhChon.COLUMN_diem_binh_chon,
				binhChonObject.getDiem_binh_chon());
		binhchon.put(QueriesConstant.TableBinhChon.COLUMN_nhan_xet,
				binhChonObject.getNhan_xet());
		binhchon.saveInBackground();
	}

	/**
	 * Create new binh chon that diadiem's existed
	 * 
	 * @param binhChonObject
	 * @param diadiem
	 * @param callBack
	 */
	public static void createNewBinhChon(BinhChonObject binhChonObject,
			final ParseObject diadiem,
			final CallBackFromParse<Object, ParseObject> callBack) {
		final ParseObject binhchon = new ParseObject(
				QueriesConstant.TableBinhChon.Table_Name);
		binhchon.put(QueriesConstant.TableBinhChon.COLUMN_nguoi_binh_chon,
				ParseUser.getCurrentUser());
		binhchon.put(QueriesConstant.TableBinhChon.COLUMN_dia_diem, diadiem);
		binhchon.put(QueriesConstant.TableBinhChon.COLUMN_diem_binh_chon,
				binhChonObject.getDiem_binh_chon());
		binhchon.put(QueriesConstant.TableBinhChon.COLUMN_nhan_xet,
				binhChonObject.getNhan_xet());
		binhchon.saveInBackground(new SaveCallback() {
			@Override
			public void done(ParseException e) {
				if (e == null) {
					callBack.done("", binhchon);
					calculateDiemBinhChon(diadiem, null);
				} else
					callBack.done("", null);
			}
		});
	}

	/**
	 * To update binh chon table
	 * 
	 * @param diadiem
	 * @param binhChonObject
	 * @param callBack
	 */
	public static void updateBinhChon(final ParseObject diadiem,
			final BinhChonObject binhChonObject,
			final CallBackFromParse<Object, ParseObject> callBack) {
		ParseQuery<ParseObject> query = ParseQuery
				.getQuery(QueriesConstant.TableBinhChon.Table_Name);
		query.whereEqualTo(QueriesConstant.TableBinhChon.COLUMN_dia_diem,
				diadiem);
		query.whereEqualTo(
				QueriesConstant.TableBinhChon.COLUMN_nguoi_binh_chon,
				ParseUser.getCurrentUser());
		query.findInBackground(new FindCallback<ParseObject>() {
			@Override
			public void done(List<ParseObject> binhchons, ParseException e) {
				if (e == null && binhchons.size() > 0) {
					final ParseObject binhchon = binhchons.get(0);
					binhchon.put(
							QueriesConstant.TableBinhChon.COLUMN_diem_binh_chon,
							binhChonObject.getDiem_binh_chon());
					binhchon.put(QueriesConstant.TableBinhChon.COLUMN_nhan_xet,
							binhChonObject.getNhan_xet());
					binhchon.saveInBackground(new SaveCallback() {
						@Override
						public void done(ParseException e) {
							callBack.done("", binhchon);
							calculateDiemBinhChon(diadiem, null);
						}
					});
				} else {
					callBack.done("", null);
					Logger.logMessage("Error updateBinhChon  ");
				}
			}
		});

	}

	/**
	 * To calculate diem binh chon from Binh chon Table
	 * 
	 * @param diadiem
	 * @param callBack
	 */
	public static void calculateDiemBinhChon(final ParseObject diadiem,
			final CallBackFromParse<Object, Object> callBack) {
		ParseQuery<ParseObject> query = ParseQuery
				.getQuery(QueriesConstant.TableBinhChon.Table_Name);
		query.whereEqualTo(QueriesConstant.TableBinhChon.COLUMN_dia_diem,
				diadiem);
		query.findInBackground(new FindCallback<ParseObject>() {
			@Override
			public void done(List<ParseObject> binhchons, ParseException e) {
				if (e == null) {
					int size = binhchons.size();
					int sum = 0;
					for (int i = 0; i < size; i++) {
						sum += binhchons
								.get(i)
								.getInt(QueriesConstant.TableBinhChon.COLUMN_diem_binh_chon);
					}
					double diem_binh_chon = ((double) sum) / size;

					diadiem.put(
							QueriesConstant.TableDiaDiem.COLUMN_diem_binh_chon,
							RoundDouble.round(diem_binh_chon, 1));
					diadiem.saveInBackground();
					// callBack.done("", RoundDouble.round(diem_binh_chon, 1));
					Logger.logMessage("diem binh chon double : "
							+ diem_binh_chon);
				} else {
					callBack.done("", 0);
					Logger.logMessage("Error calculateDiemBinhChon  ");
				}
			}
		});
	}

	/**
	 * To get list of binh chon from diadiem
	 * 
	 * @param diadiem
	 */
	public static void getBinhChon(ParseObject diadiem,
			final CallBackFromParse<Object, List<ParseObject>> callBack) {
		ParseQuery<ParseObject> query = ParseQuery
				.getQuery(QueriesConstant.TableBinhChon.Table_Name);
		query.whereEqualTo(QueriesConstant.TableBinhChon.COLUMN_dia_diem,
				diadiem);
		query.whereNotEqualTo(
				QueriesConstant.TableBinhChon.COLUMN_nguoi_binh_chon,
				ParseUser.getCurrentUser());
		query.include(QueriesConstant.TableBinhChon.COLUMN_nguoi_binh_chon);
		query.findInBackground(new FindCallback<ParseObject>() {
			@Override
			public void done(List<ParseObject> binhchons, ParseException e) {
				if (e == null)
					callBack.done("", binhchons);
				else
					callBack.done("", null);
			}
		});

	}

	/**
	 * To get binh chon of current user
	 * 
	 * @param diadiem
	 * @param callBack
	 */
	public static void getBinhChonOfCurrentUser(ParseObject diadiem,
			final CallBackFromParse<Object, ParseObject> callBack) {
		ParseQuery<ParseObject> query = ParseQuery
				.getQuery(QueriesConstant.TableBinhChon.Table_Name);
		query.whereEqualTo(QueriesConstant.TableBinhChon.COLUMN_dia_diem,
				diadiem);
		query.whereEqualTo(
				QueriesConstant.TableBinhChon.COLUMN_nguoi_binh_chon,
				ParseUser.getCurrentUser());
		query.findInBackground(new FindCallback<ParseObject>() {
			@Override
			public void done(List<ParseObject> binhchons, ParseException e) {
				if (e == null && binhchons.size() > 0)
					callBack.done("", binhchons.get(0));
				else
					callBack.done("", null);
			}
		});
	}
}
